package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jvyou.app.common.domain.dto.MenuDto;
import com.jvyou.app.common.domain.entity.Menu;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.domain.vo.MenuTreeVo;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author 橘柚
 * @since 2025-03-05 05:48:21
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/list")
    @SaCheckPermission("system:menu:list")
    public Result<List<Menu>> list(@ModelAttribute MenuDto menuDto) {
        return Result.success(menuService.listMenu(menuDto));
    }

    @GetMapping("/page")
    @SaCheckPermission("system:menu:page")
    public Result<PageResult<Menu>> page(@ModelAttribute MenuDto menuDto) {
        return Result.success(menuService.pageMenu(menuDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("system:menu:get")
    public Result<Menu> selectOne(@PathVariable Serializable id) {
        return Result.success(menuService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param menu 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("system:menu:insert")
    public Result<Menu> insert(@RequestBody Menu menu) {
        boolean success = menuService.save(menu);
        return success ? Result.success(menu) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param menu 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("system:menu:update")
    public Result<Menu> updateById(@RequestBody Menu menu) {
        boolean success = menuService.updateById(menu);
        return success ? Result.success(menu) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("system:menu:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = menuService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }

    /**
     * 获取菜单树
     *
     * @return 菜单树
     */
    @GetMapping("/tree")
    @SaCheckPermission("system:menu:tree")
    public Result<List<MenuTreeVo>> tree(@RequestParam(required = false, defaultValue = "") String name,
                                         @RequestParam(required = false) Long roleId) {
        return Result.success(menuService.treeMenu(name,roleId));
    }
}

